using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

[System.Serializable]
public class RewardItemIcon 
{
	public int category;
	public int boxId;
	public int count;
	public GameObject iconObj;
	public bool isSelected;
	public bool isChosen;
	
	public RewardItemIcon(int ca, int b, int c)
	{
		category = ca;
		boxId = b;
		count = c;
		isSelected = false;
		isChosen = false;
	}
	
	public void Select(bool bNoMoreSelect)
	{
		if( !isSelected && bNoMoreSelect)
			return;
		
		isSelected = !isSelected;
		float alpha = 0.5f;
		if(isSelected)
			alpha = 1.0f;
		foreach( Transform child in iconObj.transform)
		{
			if(child.gameObject.name == "icon")
			{
				UISprite iconSprite = child.gameObject.GetComponent<UISprite>();
				iconSprite.alpha = alpha;
			}
			else if(child.gameObject.name == "rewarditem_num")
			{
				UILabel lblNum = child.gameObject.GetComponent<UILabel>();
				lblNum.alpha = alpha;
			}
			else if(child.gameObject.name == "reward_select")
			{
				child.gameObject.SetActive(isSelected);
			}
		}			
	}
}

public class GameResultUI : MonoBehaviour {
	
	private static GameResultUI instance;

	public static GameResultUI Instance{
		get{
			return instance;
		}
	}
	
	void Awake()
	{
		instance = this;
	}
	
	public UILabel rewardRatio;
	public GameObject rewardIconPrefab;
	public GameObject itemGrid;
	public int selectMaxCount = 5;
	public int nExp;
	public int selectCurCount = 0;
	
	public List<RewardItemIcon> rewardItemList = new List<RewardItemIcon>();
	
	// Use this for initialization
	void Start () {
	
	}
	
	public void Init(int _exp, int _maxcount)
	{
		nExp = _exp;
		selectMaxCount = _maxcount;
		
	}
	
	public void SendSelectedReward()
	{
		int selectedCount = 0;
		int maxCount = selectMaxCount;
		for(int i=0; i<rewardItemList.Count; i++)
		{
			if( rewardItemList[i].isSelected)
			{
				selectedCount++;
				rewardItemList[i].isChosen = true;
			}
		}		
		
		if(rewardItemList.Count< selectMaxCount)
			maxCount = rewardItemList.Count;
		
		if(selectedCount < maxCount)
		{
			for(int i=0; i<rewardItemList.Count; i++)
			{
				if( rewardItemList[i].isChosen == false)
				{
					rewardItemList[i].isChosen = true;
					selectedCount++;
					if(selectedCount == maxCount)
						break;
				}
			}
		}
		glog.debug("selectedcount="+selectedCount+"  maxCount="+maxCount);
		
        PC_OP_SELECT_REWARDS packet = new PC_OP_SELECT_REWARDS();

        packet.ServerID = BoardManager.Instance.boardPerson_me.SID;
		packet.SelectedCount = maxCount;
		packet.BoxCategoryList = new int[maxCount];
		packet.BoxIdList = new int[maxCount];
		int index = 0;
		for(int i=0; i<rewardItemList.Count; i++)
		{
			if( rewardItemList[i].isChosen)
			{
				packet.BoxCategoryList[index] = rewardItemList[i].category;
				packet.BoxIdList[index] = rewardItemList[i].boxId;
				glog.debug("sendrewarditem category="+packet.BoxCategoryList[index]+"  boxId="+packet.BoxIdList[index]);
				index++;
			}
		}

        //CNetworkManager.Instance.SendPacket( packet.Protocol, packet.GetParam() );
		CNetworkManager.Instance.SendPacket(packet);
	}
	
	public void SelectItem(GameObject go)
	{
		bool bNoMoreSelect = false;
		if(selectCurCount == selectMaxCount)
			bNoMoreSelect = true;
		for(int i=0; i<rewardItemList.Count; i++)
		{
			if( rewardItemList[i].iconObj == go)
			{
				rewardItemList[i].Select(bNoMoreSelect);
				break;
			}
		}
		UpdateSelectCount();
	}
	
	void UpdateSelectCount()
	{
		selectCurCount = 0;
		for(int i=0; i<rewardItemList.Count; i++)
		{
			if( rewardItemList[i].isSelected)
				selectCurCount++;
		}		
		if(rewardRatio!=null)
			rewardRatio.text = selectCurCount.ToString() + "/" + selectMaxCount.ToString();
	}
	
	public void AddRewardItem(int category, int boxId, int count)
	{
		if(category == (int)BoxCategory.ITEM)
		{
			string itemname = CItemDataManager.Instance.GetItemData(boxId).name;
			RewardItemIcon itemIcon = new RewardItemIcon(category, boxId, count);
			itemIcon.iconObj = NGUITools.AddChild(itemGrid, rewardIconPrefab);
			itemIcon.iconObj.transform.Translate(0,0,rewardIconPrefab.transform.position.z);
			rewardItemList.Add( itemIcon);
			
			foreach( Transform child in itemIcon.iconObj.transform)
			{
				if(child.gameObject.name == "icon")
				{
					UISprite iconSprite = child.gameObject.GetComponent<UISprite>();
					iconSprite.spriteName = "icon_item_" + itemname;
					iconSprite.MakePixelPerfect();
					iconSprite.alpha = 0.5f;
				}
				else if(child.gameObject.name == "rewarditem_num")
				{
					UILabel lblNum = child.gameObject.GetComponent<UILabel>();
					lblNum.text = count.ToString();
					lblNum.alpha = 0.5f;
				}
				else if(child.gameObject.name == "reward_select")
				{
					//child.gameObject.GetComponent<
				}
			}
		}
		else if(category == (int)BoxCategory.UNIT)
		{
			string unitName = CUnitDataManager.Instance.GetUnitData(boxId).name;
			RewardItemIcon itemIcon = new RewardItemIcon(category, boxId, count);
			itemIcon.iconObj = NGUITools.AddChild(itemGrid, rewardIconPrefab);
			itemIcon.iconObj.transform.Translate(0,0,rewardIconPrefab.transform.position.z);
			rewardItemList.Add( itemIcon);	
			
			foreach( Transform child in itemIcon.iconObj.transform)
			{
				if(child.gameObject.name == "icon")
				{
					UISprite iconSprite = child.gameObject.GetComponent<UISprite>();
					iconSprite.spriteName = unitName;
					iconSprite.MakePixelPerfect();
					iconSprite.alpha = 0.5f;
					child.gameObject.transform.localScale = new Vector3( child.gameObject.transform.localScale.x*0.5f, child.gameObject.transform.localScale.y*0.5f, child.gameObject.transform.localScale.z);
				}
				else if(child.gameObject.name == "rewarditem_num")
				{
					child.gameObject.SetActive(false);
				}
				else if(child.gameObject.name == "reward_select")
				{
					//child.gameObject.GetComponent<
				}
			}			
		}
	}

}
